<template>
  <div class="login-page">
    <!-- 登录表单 -->
    <div class="login-container">
      <div class="login-header">
        <h1 class="login-title">登录</h1>
        <p class="login-subtitle">医疗预约挂号小程序</p>
      </div>

      <van-form @submit="handleLogin" class="login-form">
        <van-cell-group inset>
          <van-field
            v-model="loginForm.userName"
            name="userName"
            label="账号"
            placeholder="请输入您的账号"
            :rules="[{ required: true, message: '请输入账号' }]"
            left-icon="user-o"
            clearable
          />
          <van-field
            v-model="loginForm.userPass"
            type="password"
            name="userPass"
            label="密码"
            placeholder="请输入密码"
            :rules="[{ required: true, message: '请输入密码' }]"
            left-icon="lock"
            clearable
          />
        </van-cell-group>

        <div class="forgot-password">
          <span class="forgot-text">忘记密码请联系管理员</span>
        </div>

        <div class="login-button-container">
          <van-button
            round
            block
            type="primary"
            native-type="submit"
            :loading="isLoading"
            class="login-button"
          >
            {{ isLoading ? '登录中...' : '登录' }}
          </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { Toast } from 'vant'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

// 登录表单数据
const loginForm = reactive({
  userName: '',
  userPass: ''
})

// 加载状态
const isLoading = ref(false)

// 登录处理
const handleLogin = async () => {
  if (!loginForm.userName || !loginForm.userPass) {
    Toast('请输入账号和密码')
    return
  }

  isLoading.value = true

  try {
    const result = await userStore.login(loginForm)
    
    if (result.success) {
      Toast('登录成功')
      // 登录成功后跳转到首页
      router.push('/')
    } else {
      Toast(result.message || '登录失败')
    }
  } catch (error) {
    console.error('登录错误:', error)
    Toast('登录失败，请稍后重试')
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #1989fa 0%, #1976d2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-container {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 16px;
  padding: 40px 30px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-title {
  font-size: 28px;
  font-weight: 600;
  color: #323233;
  margin: 0 0 8px 0;
}

.login-subtitle {
  font-size: 14px;
  color: #969799;
  margin: 0;
}

.login-form {
  margin-bottom: 20px;
}

.forgot-password {
  text-align: right;
  margin: 16px 0 32px;
}

.forgot-text {
  font-size: 12px;
  color: #969799;
}

.login-button-container {
  margin-top: 20px;
}

.login-button {
  height: 48px;
  font-size: 16px;
  font-weight: 500;
  background: linear-gradient(135deg, #ff6b6b 0%, #ff8e8e 100%);
  border: none;
}

.login-button:active {
  opacity: 0.8;
}

/* 表单样式调整 */
:deep(.van-cell-group) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.van-field__label) {
  color: #323233;
  font-weight: 500;
}

:deep(.van-field__control) {
  color: #323233;
}

:deep(.van-field__control::placeholder) {
  color: #c8c9cc;
}

:deep(.van-field__left-icon) {
  color: #1989fa;
}

/* 输入框聚焦样式 */
:deep(.van-field--focused .van-field__control) {
  color: #323233;
}

:deep(.van-field--focused .van-field__left-icon) {
  color: #1989fa;
}
</style>
